/*
 * Copyright (c) 2012 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.coms514.scheduleme;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

class AsyncLoadTodayEvents extends TodayEventAsyncTask {
	static final String TAG = "AsyncLoadTodayEvents";
	
	AsyncLoadTodayEvents(TodayEventActivity todayEvent) {
		super(todayEvent);
	}

	@Override
	protected void doInBackground() throws IOException {
		CalendarList feed = client.calendarList().list().setFields(CalendarInfo.FEED_FIELDS).execute();
				
		Date now = new Date();
		  
		Time t = new Time();
		t.setToNow();
		t.hour = 0;
		t.minute = 0;
		now.setTime(t.toMillis(false));
		DateTime startTime = new DateTime(now);
		  
		t.setToNow();
		t.hour = 23;
		t.minute = 59;
		now.setTime(t.toMillis(false));
		DateTime endTime = new DateTime(now);
		
		println("startTime: " + startTime + ", endTime: " + endTime);
		
		/**/
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity.getBaseContext());
        boolean feedback = preferences.getBoolean("feedback", false);

        // if feedback is on, then get only the specified calendar's events
        if (feedback) {
            ArrayList<String> feedbackCalendar = new ArrayList<String>();
            int calendarCount = preferences.getInt("calendarCount", 0);
            for (int i = 0; i < calendarCount; i++) {
            	feedbackCalendar.add(preferences.getString("calendar" + i, null));
            }
		 
        	for (CalendarListEntry calendar : feed.getItems()) {
        		if (feedbackCalendar.contains(calendar.getSummary())) {
            		println("calendar id: " + calendar.getId() + ", summary: " + calendar.getSummary());
            		Events eventsList = client.events().list(calendar.getId()).setTimeMin(startTime).setTimeMax(endTime).execute();
    			
            		for (Event event : eventsList.getItems()) {
            			println("event id: " + event.getId() + ", summary: " + event.getSummary());
            			model.add(event);
            		}        			
        		}
        	}
        	
        //if feedback is off, then get all calendar's events
        } else {
        	for (CalendarListEntry calendar : feed.getItems()) {
        		println("calendar id: " + calendar.getId() + ", summary: " + calendar.getSummary());
        		Events eventsList = client.events().list(calendar.getId()).setTimeMin(startTime).setTimeMax(endTime).execute();
			
        		for (Event event : eventsList.getItems()) {
        			println("event id: " + event.getId() + ", summary: " + event.getSummary());
        			model.add(event);
        			
        			// 
        			println(event.toString());
        		}
        	}
        }
	}

	static void run(TodayEventActivity todayEvent) {
		new AsyncLoadTodayEvents(todayEvent).execute();
	}
	
	private void println(String msg) {
		Log.d(TAG, msg);
	}

}
